package cn.hzw.supermarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.InputStream;
import java.util.stream.IntStream;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityAboutBinding;
import cn.hzw.supermarket.databinding.ActivityShoppingContentBinding;

public class ShoppingContentActivity extends AppCompatActivity {

    private ActivityShoppingContentBinding binding;
    private Dialog dialog1;
    private Dialog dialog2;
    private ImageView image1;
    private ImageView image2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏，隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityShoppingContentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        //小图的点击事件（弹出大图）
        binding.image1.setOnClickListener(view -> {
            dialog1.show();
        });
        binding.image2.setOnClickListener(view -> {
            dialog2.show();
        });
        initView();
    }

    private void initView() {
        binding.layoutTitle.tvTitle.setText("超市简介");
        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            finish();
        });
       //展示在dialog上面的大图
        dialog1 = new Dialog(this,R.style.FullActivity);
        dialog2 = new Dialog(this,R.style.FullActivity);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
        dialog1.getWindow().setAttributes(attributes);
        dialog2.getWindow().setAttributes(attributes);
        image1 = getImageView();
        image2 = getImageView2();
        dialog1.setContentView(image1);
        dialog2.setContentView(image2);
        //大图的点击事件（点击让他消失）
        image1.setOnClickListener(view -> {
            dialog1.dismiss();
        });
        image2.setOnClickListener(view -> {
            dialog2.dismiss();
        });

    }

    private ImageView getImageView2() {
        ImageView imageView = new ImageView(this);
        //宽高
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        //imageView设置图片
        @SuppressLint("ResourceType")
        InputStream is = getResources().openRawResource(R.mipmap.end);
        Drawable drawable = BitmapDrawable.createFromStream(is,null);
        imageView.setImageDrawable(drawable);
        return imageView;
    }

    //动态的ImageView
    private ImageView getImageView() {
        ImageView imageView = new ImageView(this);
        //宽高
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        //imageView设置图片
        @SuppressLint("ResourceType")
        InputStream is = getResources().openRawResource(R.mipmap.first);
        Drawable drawable = BitmapDrawable.createFromStream(is,null);
        imageView.setImageDrawable(drawable);
        return imageView;
    }
}